package com.ten.shou.controller;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.ten.common.core.entity.CrmBanner;
import com.ten.common.core.entity.OssEntity;
import com.ten.common.core.vo.Result;
import com.ten.shou.service.BannerService;

import com.ten.shou.vo.BannerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * @author ：杨（1964958771@qq.com）
 * @date ：Created in 2022/12/10 20:08
 * @description：
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("/home/banner")
@Api(tags = "轮播接口类")
public class BannerController {
    @Autowired
    private BannerService bannerService;
    @Autowired
    private OssEntity ossEntity;

    @ApiOperation(value = "根据条件分页查询专区信息")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "current", value = "当前页码"),
                    @ApiImplicitParam(name = "pageSize", value = "每页显示的条数")
            }
    )
    @PostMapping("getAllBanner/{current}/{pageSize}")
    public Result getAllBanner(@PathVariable Integer current, @PathVariable Integer pageSize, @RequestBody BannerVo bannerVo) {
        return bannerService.queryAllBanner(current, pageSize, bannerVo);
    }

    @PostMapping("changeStatus/{id}/{isDisable}")
    public Result changeStatus(@PathVariable String id, @PathVariable Integer isDisable) {
        return bannerService.updateBottomStatus(id, isDisable);
    }

    @PostMapping("deleteBanner")
    public Result deleteBanner(@RequestBody BannerVo bannerVo) {
        return bannerService.deleteByIdBanner(bannerVo);
    }

    @PostMapping("updateBanner")
    public Result updateBanner(@RequestBody CrmBanner crmBanner) {
        return bannerService.updateBanner(crmBanner);
    }

    @PostMapping("addBanner")
    public Result addBanner(@RequestBody CrmBanner crmBanner) {
        return bannerService.insertBanner(crmBanner);
    }

    @PostMapping("uploadMsg")
    public String upload(MultipartFile file) {
        String filename = file.getOriginalFilename();

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ossEntity.getEndpoint(), ossEntity.getAccessKeyId(), ossEntity.getAccessKeySecret());
        try {
            try {
                String newFileName = UUID.randomUUID() + filename;
                ossClient.putObject(ossEntity.getBucketName(), newFileName, new ByteArrayInputStream(file.getBytes()));
                return "https://" + ossEntity.getBucketName() + "." + ossEntity.getEndpoint() + "/" + newFileName;
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }
}
